package bamin.com.bamindaijia.Activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;


import bamin.com.bamindaijia.R;
import bamin.com.bamindaijia.constant.Constant;

public class WebViewActivity extends ZjbBaseActivity implements View.OnClickListener {

    private String mTitle;
    private String mUrl;
    private WebView mWeb_charges_notes;
    private TextView mTv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        init();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initViews() {
        showLoadingDialog();
        WebSettings settings = mWeb_charges_notes.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("utf-8");
        mWeb_charges_notes.setWebChromeClient(new WebChromeClient());
        mWeb_charges_notes.setWebViewClient(new MyWebViewClient());
        mWeb_charges_notes.loadUrl(mUrl);

        mTv_title.setText(mTitle);
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageView_back).setOnClickListener(this);

    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        mTitle = intent.getStringExtra(Constant.INTENT_KEY.TITLE);
        mUrl = intent.getStringExtra(Constant.INTENT_KEY.URL);
    }

    @Override
    protected void findID() {
        mWeb_charges_notes = (WebView) findViewById(R.id.webview_charges_notes);
        mTv_title = (TextView) findViewById(R.id.tv_title);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageView_back:
                finishTo();
                break;
        }
    }
    class MyWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            cancelLoadingDialog();
            mWeb_charges_notes.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onStop()
    {
        super.onStop();
        //webview内存泄露
        if (mWeb_charges_notes != null) {
            ((ViewGroup) mWeb_charges_notes.getParent()).removeView(mWeb_charges_notes);
            mWeb_charges_notes.destroy();
            mWeb_charges_notes = null;
        }
    }
}
